<template>
  <div class="container" >
    <div class="product-header" >
        <img src="../assets/images/yzfc.png" alt="product-header" />
        <div class="product-header-text">
            <span>{{ linkItem?.title }}</span>
            <p>{{ linkItem?.desc }}</p>
            <n-tag size="small" style="border-radius: 10px;"> {{ linkItem?.category.title }} </n-tag>
        </div>
    </div>
    <div class="product-content" ref="productContent">
        <img src="../assets/images/yzfc.png" alt="product-content" />
        <div class="content">
            <button @click="handleClick">
                <UpIcon />
                访问
            </button>
        </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router';
import { getLinksAPI } from '@/service/links/links';
import { ref , onMounted, watch} from 'vue';
import type { Category } from '@/service/category/type';
import Swal from 'sweetalert2';
import gsap from 'gsap';
const router = useRouter();
const route = useRoute();
const productContent = ref(null);
const handleClick = () => {
  window.open(linkItem.value?.href);
}
interface LinkItem {
  id: number;
  title: string;
  href: string;
  categoryId: number;
  category: Category;
  images: any;
  desc: string;
}
const id = route.params.id;
const linkItem = ref<LinkItem>();



const loadData = async (id:number) => {
  try {
      const res = await getLinksAPI(id);
      linkItem.value = res.data;
      if(res.data.length === null) {
        Swal.fire({
          title: '提示',
          text: '该链接不存在',
          icon: 'warning',
        })
        router.push('/');
      }
  } catch (error) {
    console.error("获取数据时出错:", error);
    Swal.fire({
      title: '提示',
      text: '获取数据失败',
      icon: 'warning',
    })
    router.push('/');
  }
}

onMounted(async () => {
   await loadData(Number(id));
   gsap.from(productContent.value, {
    y: "100%", // 从下方 100px 开始
    opacity: 0, // 从透明开始
    duration: 1, // 动画持续时间
    stagger: 0.1, // 每个项目之间的延迟
    ease: "power2.out", // 缓动效果
});
})
watch(() => route.params.id, async (newId) => {
  loadData(Number(newId));
})




</script>
<style scoped lang="less">
.container {
  color: var(--color);
  width: 100%;
  padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  .product-header {
    width: 100%;
    font-size: 16px;
    font-weight: bold;
    padding: 10px 0;
    display: flex;
    justify-content: left;
    img {
        width: 80px;
        height: 80px;
        border-radius: 10px;
    }  
    .product-header-text {
        padding: 0 10px;
        p {
            font-size: 12px;
            color: var(--color);
        }
    }
  }
  .product-content {
    // width: 100%;
    height: 500px;
    background-color: #fff;
    margin-top: 10px;
    border-radius: 10px;
    padding: 10px;
    display: flex;
    justify-content: left;
    img {
        display: block;
        flex-basis: 50%;
        height: 100%;
        border-radius: 10px;
        margin-right: 10px;
    }
    .content {
        flex-basis: 50%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        button {
           width: 50%;
           height: 50px;
           border-radius: 10px;
           background-color: #fff;
           border: none;
           box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
           transition: all 0.3s ease;
        }
        button:hover {
            background-color: var(--color);
            color: #fff;
            cursor: pointer;
        }
    }
  }
}
@media (max-width: 768px) {
  .product-content {
    flex-direction: column;

    width: 348px;
    img {
      width: 100%;
      height: auto;
    }
    .content {
      width: 100%;
    }
  }
}
</style>

